package com.seadragon.app.ksr.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.seadragon.app.ksr.dao.CustomerDao;
import com.seadragon.app.ksr.dao.CustomerEventSubscriptionDao;
import com.seadragon.app.ksr.model.Address;
import com.seadragon.app.ksr.model.Customer;
import com.seadragon.app.ksr.model.CustomerEventSubscription;
import com.seadragon.app.ksr.model.Event;
import com.seadragon.app.ksr.service.AddressService;
import com.seadragon.app.ksr.service.CustomerService;
import com.seadragon.app.ksr.service.GeocodeService;
import com.seadragon.app.ksr.service.SearchService;
import com.seadragon.app.ksr.util.IndexManager;

@Service("customerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {
	private Logger logger = LoggerFactory.getLogger(CustomerServiceImpl.class);
	
	@Autowired
	private CustomerDao customerDao;
	
	@Autowired
	private CustomerEventSubscriptionDao customerEventSubscriptionDao;
	
	@Autowired
	private SearchService searchService;
	
	@Autowired
	private AddressService addressService;
	
	@Autowired
	private GeocodeService geocodeService;
	
	@Autowired
	private IndexManager indexManager;
	
	@Override
	public Customer update(Customer customer) {
		Address address = addressService.save(customer.getAddress());
		customer.setAddress(address);
		customerDao.update(customer);
		try{
			indexManager.insertDocument(customer);
		}catch(Exception ex){
			logger.error(ex.getMessage(), ex);
		}

		return customer;
	}

	@Override
	public void save(Customer customer) {
		Address address = addressService.save(customer.getAddress());
		customer.setAddress(address);

		customerDao.save(customer);
		try{
			indexManager.insertDocument(customer);
		}catch(Exception ex){
			logger.error(ex.getMessage(), ex);
		}
	}

	@Override
	public Customer findByEmail(String email) {
		return customerDao.findByEmail(email);
	}


	@Override
	public Customer findById(long id) {
		return customerDao.findById(id);
	}

	@Override
	public List<Customer> findAll(int offset, int pageSize) {
		return customerDao.findAll(offset, pageSize);
	}

	@Override
	public void subscribe(CustomerEventSubscription customerEventSubscription) {
		customerEventSubscriptionDao.save(customerEventSubscription);
	}

	@Override
	public void unsubscribe(CustomerEventSubscription customerEventSubscription) {
		customerEventSubscriptionDao.delete(customerEventSubscription);
	}

	@Override
	public void update(CustomerEventSubscription customerEventSubscription) {
		CustomerEventSubscription ces = customerEventSubscriptionDao.findById(customerEventSubscription.getId());
		if(ces == null){
			customerEventSubscriptionDao.save(customerEventSubscription);
		}else{
			if(!ces.getOpeningTime().equals(customerEventSubscription.getOpeningTime())){
				ces.setOpeningTime(customerEventSubscription.getOpeningTime());
			}
			if(!ces.getClosingTime().equals(customerEventSubscription.getClosingTime())){
				ces.setClosingTime(customerEventSubscription.getClosingTime());
			}
			if(!ces.getStartDate().equals(customerEventSubscription.getStartDate())){
				ces.setStartDate(customerEventSubscription.getStartDate());
			}
			if(!ces.getEndDate().equals(customerEventSubscription.getEndDate())){
				ces.setEndDate(customerEventSubscription.getEndDate());
			}
			
			if(!ces.getSubscriptionType().equals(customerEventSubscription.getSubscriptionType())){
				ces.setSubscriptionType(customerEventSubscription.getSubscriptionType());
			}
			if(ces.getAddress().getId() != customerEventSubscription.getAddress().getId()){
				ces.setAddress(customerEventSubscription.getAddress());
			}
			customerEventSubscriptionDao.update(ces);
		}
	}

	@Override
	public List<Customer> findAllSubscribers(Event event) {
		return customerEventSubscriptionDao.findAllSubscribers(event);
	}

	@Override
	public List<CustomerEventSubscription> findAllSubscriptions(Customer customer) {
		return customerEventSubscriptionDao.findAllSubscriptions(customer);
	}

	@Override
	public List<CustomerEventSubscription> findBy(Customer customer, Event event) {
		return customerEventSubscriptionDao.findBy(customer, event);
	}

	@Override
	public List<CustomerEventSubscription> findAllSubscriptions(Customer customer, int offset, int pageSize) {
		return customerEventSubscriptionDao.findAllSubscriptions(customer, offset, pageSize);
	}

}
